//
//  BSCommon.m
//  share_business
//
//  Created by 吴于凌 on 2017/8/26.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "BSCommon.h"

@implementation BSCommon


//图片
+(UIImage *)imageNamed:(NSString *)name{
    if (!name || [name isEqualToString:@""]) {
        return [UIImage imageNamed:@"home_today_tejia_default1"];
    }
    return  [UIImage imageNamed:name];
}


//加载图片
+(void)setImageWithUrl:(UIImageView *)imageView url:(NSString *)url{
    if (!url) {
        [imageView setImage:ImageNamed(@"home_today_tejia_default1")];
        return ;
    }
    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
    [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"goods_pic_default_175x175_"]];
}
//加载头像
+(void)setHeaderWithUrl:(UIImageView *)imageView url:(NSString *)url{
    if (!url) {
        [imageView setImage:ImageNamed(@"MyCloud_user_photo_default_97x97_")];
        return ;
    }
    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
    [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"MyCloud_user_photo_default_97x97_"]];
}

+(void)setImageWithUrlWithBlock:(UIImageView *)imageView url:(NSString *)url block:(nullable SDWebImageCompletionBlock)block {
    if (!url) {
        
        [imageView setImage:ImageNamed(@"goods_pic_default_175x175_")];
        return ;
    }
   
    [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"goods_pic_default_175x175_"]  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        block(image,error,cacheType,imageURL);
    }];
}


+(NSString *_Nonnull)getImageUrl:(NSString *_Nonnull)url{
//    NSString *rootUrl = IMGAGE_ROOT ;
//    return [NSString stringWithFormat:@"%@/%@",rootUrl,url];
    return url ;
}
+(NSString *_Nonnull)getTestImageUrl:(NSString *_Nonnull)url{
//    NSString *rootUrl = IMGAGE_ROOT ;
//    return [NSString stringWithFormat:@"%@/%@",rootUrl,url];
     return url ;
}
/** 返回自适应高度的文本 */

+(CGFloat)calculateRowHeight:(NSString *)string font:(CGFloat)size maxWidth:(CGFloat)maxWidth
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"Courier" size:size]};//指定字号
    CGRect rect = [string boundingRectWithSize:CGSizeMake(maxWidth, 0)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.height;
}

/** 返回自适应宽度的文本 */

+ (CGFloat)calculateRowWidth:(NSString *)string fontSize:(NSInteger)size maxHeight:(NSInteger)maxHeight{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"Courier" size:size]};  //指定字号
    CGRect rect = [string boundingRectWithSize:CGSizeMake(0, maxHeight)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.width;
}


+(UIImage*)imageCompressWithSimple:(UIImage*)image scaledToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0,0,size.width,size.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+(NSMutableAttributedString * _Nonnull)getArrtributeString:(NSString * _Nonnull)title attword:(NSString *_Nonnull)arrword color:(UIColor *_Nonnull)color{
    
    NSMutableAttributedString *mAttStri = [[NSMutableAttributedString alloc] initWithString:title];
    if (!arrword || !title) {
        return mAttStri ;
    }
    NSRange range = [title rangeOfString:arrword];
    [mAttStri addAttribute:NSForegroundColorAttributeName value:color range:range];
    
    return mAttStri ;
}

+(NSInteger)changeToInteger:(NSString *_Nonnull)str {
    if (!str || [str isEqualToString:@""]) {
        return 0 ;
    }
    if ([self isPureInt:str]) {
        NSInteger value = [str integerValue];
        return value ;
    }else{
        return 0 ;
    }
    
}

+(float)changeToFloat:(NSString *)str{
    if (!str) {
        return 0 ;
    }
    if ([self isPureFloat:str]) {
        float value = [str floatValue];
        return value ;
    }else{
        return 0 ;
    }
    
}

+(BOOL)isPureInt:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}
+(BOOL)isPureFloat:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    return [scan scanFloat:&val] && [scan isAtEnd];
}

//md5加密
+ (NSString *) md5:(NSString *) input {
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return  output;
}


 

+(void)moveImgTo:(UIImageView *_Nonnull)img x:(float)x y:(float)y {
    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    CGRect rect=[img convertRect: img.bounds toView:window];
    
    UIImageView *view  = [[UIImageView alloc]initWithFrame:CGRectMake(rect.origin.x+img.width/2-40, rect.origin.y+img.height /2-40, 80, 80)];
    view.image = img.image ;
    [view setBorder:0 cornerRadius:40];
    view .layer.masksToBounds =YES ;
    [window addSubview:view];
    [view bringSubviewToFront:window];
    
    float toX = x - (rect.origin.x+img.width/2-40) ;
    float toY = y - (rect.origin.y+img.height /2-40) ;
    
    [UIView animateWithDuration:1 animations:^{
        [view setTransform:(CGAffineTransformMakeTranslation(toX,toY))];
        [view setTransform:CGAffineTransformScale(view.transform, 0.2, 0.2)];
        
    } completion:^(BOOL finished) {
        // 在这里再调用这个方法本身
        view.hidden = YES ;
        [view removeFromSuperview];
    }];
}

+(BOOL) isBlankString:(NSString *)string {
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}
 

@end
